package com.example.controller;


import com.example.common.Result;
import com.example.entity.MriImages;
import com.example.exception.CustomException;
import com.example.service.MriImagesService;
import com.example.service.NiiFileService;
import com.example.utils.OSSUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * MRI影像表前端操作接口
 **/
@Slf4j
@RestController
@RequestMapping("/mriImages")
public class MriImagesController {

    @Autowired
    private MriImagesService mriImagesService;
    @Autowired
    private NiiFileService niiFileService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody MriImages mriImages) {
        mriImagesService.add(mriImages);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        mriImagesService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        mriImagesService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody MriImages mriImages) {
        mriImagesService.updateById(mriImages);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        MriImages mriImages = mriImagesService.selectById(id);
        return Result.success(mriImages);
    }

    /**
     * 根据文件名查询
     */
    @GetMapping("/selectByFileName/{fileName}/{userId}")
    public Result selectById(@PathVariable String fileName, @PathVariable Integer userId) {
        MriImages mriImages = mriImagesService.selectByFileNameAndUserId(fileName, userId);
        return Result.success(mriImages);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(MriImages mriImages) {
        List<MriImages> list = mriImagesService.selectAll(mriImages);
        return Result.success(list);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(MriImages mriImages,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<MriImages> page = mriImagesService.selectPage(mriImages, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 获取png图片列表
     */
    @GetMapping("/getPngList/{seed}/{imageName}")
    public Result getPngList(@PathVariable String seed, @PathVariable String imageName) {
        List<List<String>> pngList = mriImagesService.getPngList(seed, imageName);
        return Result.success(pngList);
    }

    /**
     * 根据imageId获取到文件的路径
     */
    @GetMapping("/downloadNiiFile/{imageId}")
    public Result download(@PathVariable Integer imageId) {
        return Result.success(niiFileService.getNiiURL(imageId));
    }
}